const { readFile, writeFile, mkdir, readdirSync } = require("fs");

// 创建文件夹
function  f_mkdir(path, options) {
  if (!options) {
    options = { recursive: true };
  }
  return new Promise((resolve, reject) => {
    mkdir(path, options, (err) => {
      if (err) {
        reject({ code: 0, err, msg: "创建文件夹失败" });
        return;
      }
      resolve({ code: 1, msg: "创建成功" });
    });
  });
}

// 数据写入
function f_dataWriteFile(path, data, options) {
  if (typeof data !== "string") {
    data = JSON.stringify(data);
  }
  return new Promise((resolve, reject) => {
    // 写入数据
    writeFile(path, JSON.stringify(data), (err) => {
      if (err) {
        reject({ code: 0, err, msg: "操作失败" });
        return;
      }
      // 响应
      resolve({ code: 1, msg: "操作成功" });
    });
  });
}
